from apps.orders.models import Order
from django.shortcuts import render
from rest_framework.views import APIView
from django.http import JsonResponse
from apps.house.models import House
import datetime
from utils.user import UserName


# Create your views here.
# 切换订单
class OrdersView(UserName, APIView):
    def post(self, request):
        data = request.data
        house_id = data['house_id']
        start_date = data['start_date']
        end_date = data['end_date']

        house_price = House.objects.get(id=house_id).price

        end = datetime.datetime.strptime(end_date, "%Y-%m-%d")
        start = datetime.datetime.strptime(start_date, "%Y-%m-%d")

        days = (end - start).days
        amount = house_price * days
        order = Order.objects.create(
            house_id=house_id,
            begin_date=start,
            days=days,
            end_date=end,
            amount=amount,
            user=request.user,
            house_price=house_price,
            status=Order.ORDER_STATUS['WAIT_ACCEPT']
        )

        return JsonResponse({
            "data": {
                "order_id": order.id
            },
            "errno": "0",
            "errmsg": "下单成功"
        })

    # 获取订单列表
    def get(self, request):
        role = request.GET.get('role')
        list1 = []
        if role == 'custom':
            objs = Order.objects.filter(user_id=request.user.id)
        elif role == 'landlord':
            users = House.objects.filter(user_id=request.user.id)
            for i in users:
                list1.append(i.id)
            objs = Order.objects.filter(house_id__in=list1)

        list2 = []
        for i in objs:
            if i.house.index_image_url:
                str1 = 'http://s5yc6o2h0.hd-bkt.clouddn.com/' + i.house.index_image_url
            else:
                str1 = ''
            list2.append({
                "amount": i.amount,
                "comment": i.comment,
                "ctime": i.create_time,
                "days": i.days,
                "end_date": i.end_date,
                "img_url": str1,
                "order_id": i.id,
                "start_date": i.begin_date,
                "status": Order.ORDER_STATUS_ENUM[i.status],
                "title": i.house.title
            })
        return JsonResponse({
            "data": {
                "orders": list2
            },
            "errmsg": "OK",
            "errno": "0"
        })


class OrdersAcTion(UserName, APIView):
    def put(self, request, order_id):
        action = request.data.get('action')
        reject = request.data.get('reject')

        if action == "accept":
            obj = Order.objects.get(id=order_id)
            if obj.status != Order.ORDER_STATUS['WAIT_ACCEPT']:
                return JsonResponse({"errno": "4103", "errmsg": "状态不是待接单"})
            else:
                obj.status = Order.ORDER_STATUS['WAIT_COMMENT']
                obj.save()
                return JsonResponse({"errno": "0", "errmsg": "接单成功"})
        elif action == "reject":
            obj = Order.objects.get(id=order_id)
            obj.status = Order.ORDER_STATUS['REJECTED']
            obj.comment = request.data.get('reason')
            obj.save()
            return JsonResponse({"errno": "0", "errmsg": "拒单成功"})
        else:
            return JsonResponse({"errno": "4103", "errmsg": "请正确输入"})


class OrdersCommit(UserName, APIView):
    def put(self, request, order_id):
        comment = request.data.get('comment')
        obj = Order.objects.get(id=order_id)
        obj.comment = comment
        obj.save()
        return JsonResponse({
            "errno": "0",
            "errmsg": "评论成功"
        })
